/***************************************************************************
 创建者: 华磊
 开始时间: 2020.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2020.5.8 类的具体实现代码编写

 ***************************************************************************
 *  @file x.h
 *  功能:
 *
 *                                                                         *
 ***************************************************************************/
#ifndef LOGICMOTOR_H
#define LOGICMOTOR_H

#include "ecattype.h"
#include "stdio.h"
#include "GeneralDefine.h"


/**
 * @brief The EcatMotor class 电机抽象类
 */
class LogicMotor
{
public:
    LogicMotor(E_DEVICE_PROTOCOL protocolIn, int aliasIn,int busIdIn);


public:
    int checkEthercatDeviceError( bool &isWarningState,bool &isFaultOccurred,
                                  unsigned int &errorCodeOut,unsigned int &vendorIdOut,int &aliasOut);
    int checkCanopenDeviceError( bool &isWarningState,bool &isFaultOccurred,
                                  unsigned int &errorCodeOut,unsigned int &vendorIdOut,int &aliasOut);
    void writeActVelocity(int actVelocityIn);
    int setSdoList(QVector<SdoConfig> &sdoConfigListIn);
    int getSdoList(QVector<SdoConfig> &sdoConfigListOut);
    E_MOTOR_CMD getMotorStateCommand();
    E_MOTOR_CONTROL_MODE getControlMode();
    int setControlMode(E_MOTOR_CONTROL_MODE controlModeIn);
    int getVendorId();//vendor id,要等到materPreOp才能加载进去
    int setVendorId(int vendorIdIn);
    int setZeroReturnParameter(ZeroReturnParameter zeroReturnParaIn);
    int changeControlMode(E_MOTOR_CONTROL_MODE controlModeIn);
    int getAlias();
    int getProtocol();

    bool isServoOn(  );
    bool isServoOn_ethercat(  );
    bool isServoOn_modbus(  );


    bool isServoQuickStop(   );
    bool isServoQuickStop_ethercat(   );
    bool isServoQuickStop_modbus(   );


    bool isServoPowerOn(  );
    bool isServoPowerOn_ethercat(  );
    bool isServoPowerOn_modbus(  );


    bool isServoFllowingError(  );
    bool isServoFllowingError_ethercat(  );
    bool isServoFllowingError_modbus(  );


    bool isServoTargetReached(  );
    bool isServoTargetReached_ethercat(  );
    bool isServoTargetReached_modbus(  );

//    int changeToCsvMode();
//    int changeToCsvMode_ethercat();
//    int changeToCsvMode_modbus();

    int changeToHomingMode();
    int changeToHomingMode_ethercat(  );
    int changeToHomingMode_modbus(  );

    bool startGoHome(  );
    bool startGoHome_ethercat(  );
    bool startGoHome_modbus(  );

    bool haltGoHome(  );
    bool haltGoHome_ethercat(  );
    bool haltGoHome_modbus(  );

    int getZeroMovingStatus(EM_ZERO_MOVING_STATUS &statusOut);
    int getZeroMovingStatus_ethercat(EM_ZERO_MOVING_STATUS &statusOut);
    int getZeroMovingStatus_modbus(EM_ZERO_MOVING_STATUS &statusOut);

    int restorNormalOperationMode();
    int restorNormalOperationMode_ethercat();
    int restorNormalOperationMode_modbus();

    bool isServoHoming(  );
    bool isServoHoming_ethercat(  );
    bool isServoHoming_modbus(  );


    bool isServoHomingOk(  );
    bool isServoHomingOk_ethercat(  );
    bool isServoHomingOk_modbus(  );


    bool isServoHomingErr(  );
    bool isServoHomingErr_ethercat(  );
    bool isServoHomingErr_modbus(  );


//    int getState( ){ return controlWord; }
    void setControlWordDebug(unsigned short controlWordIn);
    void setMotorState(E_MOTOR_CMD cmd);
    void setEthercatMotorState(E_MOTOR_CMD cmd);
    void setModbusMotorState(E_MOTOR_CMD cmd);
    void updateMotorState();
    void updateEthercatMotorState();
    void updateModbusMotorState();


public:
    /*--PDO_OUTPUT-----------------------------------------*/
   volatile unsigned short controlWord;     // 0x6040 - 控制字
    unsigned char  modeOfOperation; // 0x6060 - 控制模式
    unsigned char  modeOfOperation_old; // 0x6060 - 控制模式
    int	targetPosition; // 0x607A - 目标位置 csp
    short targetTorque; // 0x6071 - 目标力距 cst
    int targetVelocity; // 0x60FF - 目标速度 csv
    short torqueOffset; // 0x60B2 - 力距漂移
    int	velocityOffset; // 0x60B1 - 速度漂移
    double pulsePerRound;//电机转１圈对应编码器的脉冲数　　     （２５００线编码器对应１００００脉冲)


    /*--PDO_INPUT------------------------------------------*/
    unsigned int	errorCode; // 0x603F - 错误码 u16
    unsigned short	stateWord; // 0x6041 - 状态字 u16
    int	actPosition;  // 0x6064 - 位置反馈 s32
    int	actVelocity;  // 0x606C - 速度反馈 s32
    short actTorque;  // 0x6077 - 力矩反馈 s16
    int	actFollowErr; // 0x60F4 - 跟随误差 s32
    unsigned char disModeOfOperation; // 0x6061 - 控制模式显示 s8
    unsigned int digIn; // 0x60FD - 数字输入 u32
    unsigned int digOut;// 0x60FE - 数字输out u32
    int externalFeedback;

    /*--funtions-------------------------------------------*/
    // 计算的编码器的值
    int encoderValue;
    //原点偏移
    int homeOffset;


public:
    E_DEVICE_PROTOCOL protocol;
    int alias;
    int busId;
    volatile unsigned int vendorId;

private:
    E_MOTOR_CONTROL_MODE controlMode;
    E_MOTOR_CONTROL_MODE controlMode_old;
    E_MOTOR_CMD motorStateCommand;//电机命令状态
    ZeroReturnParameter zeroReturnPara;
    QVector<SdoConfig> sdoConfigList;//ethercat使用
    int tmpTickCount;//驱动器第一次启动，好像会误报警告：不支持设定控制模式




};

#endif // ECATMOTOR_H
